﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Infragistics.Win.UltraWinGrid;
using Infragistics.Win;

namespace WindowsFormsApplication283
{
    public partial class Form1 : Form
    {
        UltraCombo uc;
        public Form1()
        {
            InitializeComponent();

            DataTable dt = new DataTable();
            dt.Columns.Add("Int", typeof(int));
            dt.Columns.Add("Name", typeof(string));
            dt.Rows.Add(1, "Indocin1");
            dt.Rows.Add(2, "Indocin2");
            dt.Rows.Add(3, "Indocin3");

            DataTable dtt = new DataTable();
            dtt.Columns.Add("Int123", typeof(int));
            dtt.Rows.Add(2);
            dtt.Rows.Add(3);
            dtt.Rows.Add(1);

            uc = new UltraCombo();
            uc.BindingContext = this.BindingContext;

            uc.DataSource = dt;
            //uc.SetDataBinding(dtnew, null);
            uc.ValueMember = "Int";
            uc.DisplayMember = "Name";


            //ultraGrid1.DataSource = dtt.DefaultView;

            ultraGrid1.DataSource = GetTable();

        }

        static DataTable GetTable()
        {
            //
            // Here we create a DataTable with four columns.
            //
            DataTable table = new DataTable();
            table.Columns.Add("Dosage", typeof(int));
            table.Columns.Add("Drug", typeof(string));
            table.Columns.Add("Patient", typeof(string));
            table.Columns.Add("Date", typeof(DateTime));
            table.Columns.Add("type", typeof(bool));

            //
            // Here we add five DataRows.
            //
            table.Rows.Add(1, "Indocin", "David", DateTime.Now,true);
            table.Rows.Add(2, "Enebrel", "Sam", DateTime.Now,false);
            table.Rows.Add(3, "Hydralazine", "Christoff", DateTime.Now);
            table.Rows.Add(2, "Combivent", "Janet", DateTime.Now);
            table.Rows.Add(1, "Dilantin", "Melanie", DateTime.Now);
            return table;
        }

        private void ultraGrid1_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Up:
                    ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false, false);
                    ultraGrid1.PerformAction(UltraGridAction.AboveCell, false, false);
                    e.Handled = true;
                    ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false, false);
                    break;
                case Keys.Down:
                    ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false, false);
                    ultraGrid1.PerformAction(UltraGridAction.BelowCell, false, false);
                    e.Handled = true;
                    ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false, false);
                    break;
                case Keys.Right:
                    ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false, false);
                    ultraGrid1.PerformAction(UltraGridAction.NextCellByTab, false, false);
                    e.Handled = true;
                    ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false, false);
                    break;
                case Keys.Left:
                    ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false, false);
                    ultraGrid1.PerformAction(UltraGridAction.PrevCellByTab, false, false);
                    e.Handled = true;
                    ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false, false);
                    break;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //ultraGrid1.DisplayLayout.Override.AllowAddNew = AllowAddNew.FixedAddRowOnBottom;
            UltraGridRow row1 = this.ultraGrid1.DisplayLayout.Bands[0].AddNew();
            //row.Cells[0].Value = "Red";
            //row.Cells[1].Value = "1";
            //row.Cells[2].Value = Color.Red;

            #region
            UltraGridRow row = this.ultraGrid1.Rows[1];

            UltraGridColumn column = this.ultraGrid1.DisplayLayout.Bands[0].Columns[1];
            var a = column.MaxLength;
            // UltraGridCell.Value property returns the same value as returned by GetCellValue below.
            object cellVal1 = row.Cells[column].Value;

            // UltraGridRow.GetCellValue method returns the cell's value.
            object cellVal2 = row.GetCellValue(column);
            string cellText1 = row.Cells[column].Text;

            // UltraGridRow.GetCellText method returns the cell's value converted to text.
            string cellText2 = row.GetCellText(column);

            //MessageBox.Show("UltraGridCell.Value		= " + cellVal1.ToString());
            //MessageBox.Show("UltraGridRow.GetCellValue	= " + cellVal2.ToString());
            //MessageBox.Show("UltraGridCell.Text		= " + cellText1);
            //MessageBox.Show("UltraGridRow.GetCellText	= " + cellText2);
            #endregion



        }
        private void BindUltraDropDown()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ID", typeof(int));
            dt.Columns.Add("DisplayText", typeof(string));

            dt.Rows.Add(new object[] { 1, "A" });
            dt.Rows.Add(new object[] { 2, "B" });
            dt.Rows.Add(new object[] { 3, "C" });
            dt.AcceptChanges();

            this.uc.SetDataBinding(dt, null);
            this.uc.ValueMember = "ID";
            this.uc.DisplayMember = "DisplayText";
        }

        private void ultraGrid1_InitializeLayout(object sender, InitializeLayoutEventArgs e)
        {
            e.Layout.Bands[0].Columns[0].EditorComponent = uc;

            UltraGridColumn ugc = ultraGrid1.DisplayLayout.Bands[0].Columns[4];
            ugc.Style = Infragistics.Win.UltraWinGrid.ColumnStyle.CheckBox;
            ugc.CellActivation = Activation.AllowEdit;
            //Disable all record
            //this.ultraGrid1.DisplayLayout.Bands[0].Override.AllowUpdate = DefaultableBoolean.False;
            //Disable columns
            //this.ultraGrid1.DisplayLayout.Bands[0].Columns[1].CellActivation = Activation.Disabled;
        }

        private void ultraGrid1_InitializeLayout_1(object sender, InitializeLayoutEventArgs e)
        {
            e.Layout.Bands[0].Columns[0].EditorComponent = uc;
            //ultraGrid1.DisplayLayout.Bands[0].Columns[0].CellActivation = Activation.Disabled;
            ultraGrid1.DisplayLayout.Bands[0].Columns[1].Style = Infragistics.Win.UltraWinGrid.ColumnStyle.Button;

            //this.ultraGrid1.Rows.Band.Columns[0].ButtonDisplayStyle = Infragistics.Win.UltraWinGrid.ButtonDisplayStyle.Always;

            //this.ultraGrid1.DisplayLayout.ViewStyleBand = ViewStyleBand.OutlookGroupBy;
            //this.ultraGrid1.DisplayLayout.Bands[0].SortedColumns.Add("Dosage", false, true);
            //this.ultraGrid1.Rows.ExpandAll(true);
            //Disable all record
            //this.ultraGrid1.DisplayLayout.Bands[0].Override.AllowUpdate = DefaultableBoolean.False;
            //Disable columns
            //this.ultraGrid1.DisplayLayout.Bands[0].Columns[1].CellActivation = Activation.Disabled;
        }

        private void ultraGrid1_CellChange(object sender, CellEventArgs e)
        {
            //if (!StringComparer.OrdinalIgnoreCase.Equals(e.Cell.Column.Index, 4))
            //{
            //    //MessageBox.Show("checkbox");
            //    // do something special when the checkbox value is changed
            //}
            //bool checkedState = bool.Parse(e.Cell.Text);

            //DataRowView row = e.Cell.Row.ListObject as DataRowView;
            //string name = row.Row[@"Drug"] as string;

            //MessageBox.Show(string.Format("Checked={0}, name={1}", checkedState, e.Cell.Row.ListObject));

            


        }

        private void ultraGrid1_ClickCellButton(object sender, CellEventArgs e)
        {
            UltraGrid gSender = (UltraGrid)sender;

            OpenFileDialog ofLoadIcon = new OpenFileDialog();
            ofLoadIcon.Filter = "All Pictures|*.bmp;*.gif;*.jpg;*.png|Bitmaps|*.bmp|GIFs|*.gif|JPEGs|*.jpg|PNGs|*.png";
            if (ofLoadIcon.ShowDialog() == DialogResult.OK)
            {
                e.Cell.Value = ofLoadIcon.FileName;
            }
        }

    }
}
